﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace POC.Configuration
{

    public static class DataTypeConvertExtn
    {
        public static Nullable<int> ToNullableInt(this string stringValue)
        {
            var val = int.MinValue;
            int.TryParse(stringValue, out val);
            return (val == int.MinValue) ? null : new Nullable<int>(val);
        }

        public static Nullable<long> ToNullableLong(this string stringValue)
        {
            var val = long.MinValue;
            long.TryParse(stringValue, out val);
            return (val== long.MinValue) ? null: new Nullable<long>(val);
        }
        public static int ToInt(this string stringValue)
        {
            var val = int.MinValue;
            int.TryParse(stringValue, out val);
            return val;
        }
        public static bool ToBoolean(this string stringValue)
        {
            var val = false;
            bool.TryParse(stringValue, out val);
            return val;
        }
        public static long ToLong(this string stringValue)
        {
            var val = long.MinValue;
            long.TryParse(stringValue, out val);
            return val;
        }
        public static double ToDouble(this string stringValue)
        {
            var val = double.MinValue;
            double.TryParse(stringValue, out val);
            return val;
        }
        public static decimal ToDecimal(this string stringValue)
        {
            var val = decimal.MinValue;
            decimal.TryParse(stringValue, out val);
            return val;
        }
        public static byte[] ToByte(this string stringValue)
        {
            var val = new byte[256];
            val = UTF8Encoding.UTF8.GetBytes(stringValue);
            return val;
        }
        public static string ToString(this byte[] byteValue)
        {
            var val = string.Empty;
            val = UTF8Encoding.UTF8.GetString(byteValue);
            return val;
        }
        public static bool Contains(this IDictionary<string, string> dictionary, string key)
        {
            var kp = (from k in dictionary where k.Key.ToLower() == key.ToLower() select k);
            return kp.Any();
        }
        public static string Get(this IDictionary<string, string> dictionary, string key)
        {
            var kp = (from k in dictionary where k.Key.ToLower() == key.ToLower() select k);
            if (kp.Any())
            {
                return kp.FirstOrDefault().Value;
            }
            else
            {
                return null;
            }
        }
    }
}
